﻿using System.Data.Entity;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NQueries.TestSupport;
using Rhino.Mocks;

namespace NQueries.Data.Test
{
    [TestClass]
    public class DbSetExtensionsTest
    {
        public TestContext TestContext { get; set; }

        [TestInitialize]
        public void TestInitialize() { MockTools.Reset(); }

        [TestMethod]
        public void GetByIdTest()
        {
            IDbSet<object> set = MockTools.Strict<IDbSet<object>>();
            object id = 42;
            object expected = new object();

            using (MockTools.Record())
                Expect.Call(set.Find(id)).Return(expected);

            using (MockTools.Playback())
                Assert.AreEqual(expected, set.GetById(id));
        }

        [TestMethod]
        public void GetByIdFailedTest()
        {
            IDbSet<object> set = MockTools.Strict<IDbSet<object>>();
            object id = 42;

            using (MockTools.Record())
                Expect.Call(set.Find(id)).Return(null);

            using (MockTools.Playback())
                TestTools.AssertThrows<NQueriesException>(() => set.GetById(id));
        }

        [TestMethod]
        public void UpdateTest()
        {
            IDbSet<object> set = MockTools.Strict<IDbSet<object>>();
            object entity = new object();

            IDataContext context = MockTools.StrictDataContext();
            using (MockTools.Record())
            {
                Expect.Call(() => context.Update(entity));
                Expect.Call(context.Dispose);
            }

            using (MockTools.Playback())
                using (UnitOfWork.Begin())
                    set.Update(entity);
        }
    }
}